package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.SkuImagesEntity;
import com.atguigu.gulimail.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimail.product.service.*;
import com.atguigu.gulimail.product.vo.SkuInfoVo;
import com.atguigu.gulimail.product.vo.SpuItemGroupVo;
import com.atguigu.gulimail.product.vo.SpuSaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.SkuInfoDao;
import com.atguigu.gulimail.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        /**
         * key: '华为',//检索关键字
         * catelogId: 0,
         * brandId: 0,
         * min: 0,
         * max: 0
         */
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(w -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            queryWrapper.eq("catalog_id", catelogId);

        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(key) && !"0".equalsIgnoreCase(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        BigDecimal minPrice = new BigDecimal(min);
        if (!StringUtils.isEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)) {
            //max 的值大于min的值
            BigDecimal maxPrice = new BigDecimal(max);
            if (maxPrice.compareTo(minPrice) == 1) {
                queryWrapper.le("price", max);
            }

        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public SkuInfoVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuInfoVo skuInfoVo = new SkuInfoVo();
        // 1、sku基本属性获取
        //使用异步编排的方式。

        CompletableFuture<SkuInfoEntity> sku_info = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info = this.baseMapper.selectById(skuId);
            if (info!=null){
                skuInfoVo.setInfo(info);
                return info;
            }else{
                return null;
            }

        }, executor);


        CompletableFuture<Void> sku_image = CompletableFuture.runAsync(() -> {
            //2、sku图片信息
            List<SkuImagesEntity> skuImagesEntityList = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuInfoVo.setImages(skuImagesEntityList);
        }, executor);

        CompletableFuture<Void> sku_attr = sku_info.thenAcceptAsync(sku -> {
            //3、spu销售属性组合
            List<SpuSaleAttrVo> saleAttr = skuSaleAttrValueService.getSaleAttrGroup(sku.getSpuId());
            skuInfoVo.setSaleAttr(saleAttr);
        }, executor);


        CompletableFuture<Void> sku_desc = sku_info.thenAcceptAsync(sku -> {
            //4、获取spu的介绍
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(sku.getSpuId());
            skuInfoVo.setDesp(spuInfoDescEntity);
        }, executor);

        CompletableFuture<Void> sku_base = sku_info.thenAcceptAsync(sku -> {
            //5、spu的规格参数等信息
            List<SpuItemGroupVo> groupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(sku.getSpuId(), sku.getCatalogId());
            skuInfoVo.setGroupAttrs(groupAttrs);
        }, executor);

        //等所有都执行完。 在返回
        CompletableFuture.allOf(sku_image, sku_attr, sku_desc, sku_base).get();


        return skuInfoVo;
    }


}